<?php

namespace App\Common;

use Illuminate\Support\Facades\DB;
use App\Service\DataReturnService as DRS;

class Base{
    /**
     * @param $url 请求网址
     * @param bool $params 请求参数
     * @param int $ispost 请求方式
     * @param int $https https协议
     * @return bool|mixed
     */
    public static function curl($url, $params = false, $ispost = 0, $https = 0)
    {
        $httpInfo = array();
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if ($https) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
        }
        if ($ispost) {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
            curl_setopt($ch, CURLOPT_URL, $url);
        } else {
            if ($params) {
                if (is_array($params)) {
                    $params = http_build_query($params);
                }
                curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);
            } else {
                curl_setopt($ch, CURLOPT_URL, $url);
            }
        }

        $response = curl_exec($ch);

        if ($response === FALSE) {
            //echo "cURL Error: " . curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $httpInfo = array_merge($httpInfo, curl_getinfo($ch));
        curl_close($ch);
        return $response;
    }

    // 通过token获取用户信息(不需要绑定手机)
    public static function getUserByToken($token)
    {
        $token = DB::table('token')->where(['token_value' => $token])->first();

        if(empty($token))
        {
            return DRS::returnError(1110,'登录失效！');
        }else{
            $user = DB::table('users')->where(['id' => $token->user_id])->first();
            if(empty($user))
            {
                return DRS::returnError(1010,'该用户不存在！');
            }
            
            return DRS::returnOk((array)$user);
        }
    }

    // 通过token获取用户信息（需要绑定手机）
    public static function getUserByTokenWithPhone($token)
    {
        $token = DB::table('token')->where(['token_value' => $token])->first();

        if(empty($token))
        {
            return DRS::returnError(1110,'登录失效！');
        }else{
            $user = DB::table('users')->where(['id' => $token->user_id])->first();
            if(empty($user))
            {
                return DRS::returnError(1010,'该用户不存在！');
            }
            elseif($user->phone == null 
                || $user->phone == '' 
                || $user->phone == 0)
            {
                return DRS::returnError(1111,'未绑定手机号！');
            }
            return DRS::returnOk((array)$user);
        }
    }

    public static function getSystem_id($business_id=0)
    {
        $business = DB::table('users')->where(['id' => $business_id])->first();
        if(empty($business))
        {
            return DRS::returnError(1010,'该用户不存在！');
        }else{
            
            return DRS::returnOk((array)$business);
        }
    }
    //通过商户id获取商户信息
    public static function getBusiness_id($business_id=0)
    {
        $business = DB::table('business')->where(['business_id' => $business_id])->first();
        if(empty($business))
        {
            return DRS::returnError(1010,'该商户不存在！');
        }else{
            
            return DRS::returnOk((array)$business);
        }
    }

    /**
     * 验证手机号格式
     * @param type $phone
     */
    public static function check_mobile($phone=0)
    {
        preg_match('/^0?(13|14|15|17|18)[0-9]{9}$/', $phone, $matches);
        if(!$matches){
            $date = [
                'code'=>20002,
                'msg'=>'手机号不正确'
            ];
            return $date;
        }
    }

    //对象和数组转换
    public static function object_array($array) 
    { 
        if(is_object($array)) { 
            $array = (array)$array; 
        } 
        if(is_array($array)) { 
            foreach($array as $key=>$value) { 
                $array[$key] = self::object_array($value); 
            } 
        } 
        return $array; 
    }
    #将参数转换成k=v拼接的形式
    public static function toString($arraydata)
    {
        $Str="";
        foreach ($arraydata as $k=>$v){
            $Str .= strlen($Str) == 0 ? "" : "&";
            $Str.=$k."=".$v;
        }
        return $Str;
    }

    /**
     * @描述：存储调用易宝接口的日志在数据表
     * @param (int)$type, (string)$name, (string)$request, (string)$response
     * @return array
     * @作者：郑明军
     * @日期：2018-10-16
    **/

    public static function yibaoLog($type, $name, $request, $response)
    {
        $inserData = [
            'type'          => $type,
            'name'          => $name,
            'request'       => $request,
            'response'      => $response,
            'create_time'   => date('Y-m-d H:i:s')
        ];
        $insertLog = DB::table('yibao_log')->insert($inserData);
        if($insertLog){
            return DRS::returnOk('ok');
        }else{
            return DRS::returnError(10001,'error');
        }

    }
}